<?php
// TODO validate post input than find out how many swfs are in PLUGINS_PATH and possibly how many totalbytes they hold

// testestest


$PLUGINS_PATH = $_POST['PLUGINS_PATH'];
$totalBytes = 0;
$PLUG_LIST;

if (is_String($PLUGINS_PATH))
{
	$plugDir = scandir($PLUGINS_PATH);
	
	if($plugDir != FALSE)
	{
		// create xml
		
		$xml = new DOMDocument('1.0','utf-8');
		$root = $xml->createElement('plugins');
		$xml->appendChild($root);

		// search for swf
		
		foreach($plugDir as $file)
		{
			if(is_file($PLUGINS_PATH.$file) && substr($file,-3) == 'swf')
			{
				// swf file
				$PLUG_LIST[] = $file;
				$totalBytes += filesize($PLUGINS_PATH.$file);
				
				//xml file
				$plugin = $xml->createElement('plugin');
				$root->appendChild($plugin);
				
				$path_attr = $xml->createAttribute('path');
				$plugin->appendChild($path_attr);
				
				$path_attr_text = $xml->createTextNode($file);
				$path_attr->appendChild($path_attr_text);
			}
		}
		
		// save xml
		
		$xmlFileName = $PLUGINS_PATH.'plugins.xml';
		
		file_put_contents($xmlFileName, $xml->saveXML());
		
		
		$returnee = array(
						'plglst'=>$xmlFileName,
						'ttlbyts'=>$totalBytes,
						);
		
		echo http_build_query($returnee);
		
		
	}
	else
	{
		echo 'PHPError : The provided "PLUGINS_PATH" is not valid. Make sure it points to the rigth directory. ';
	}
	
}

else
{
	echo 'PHPError : Wrong post input type, should be string. ';
}
?>